#region Licence
// Copyright (c) 2008 Tasman Logic Ltd

// Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
// and associated documentation files (the "Software"), to deal in the Software without restriction, 
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
// subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all copies 
// or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Reflection;

namespace ActiveSharp.PropertyMapping
{
    /// <summary>
    /// Property map for a particular type.  Only instantiate this directly if you have a special need to
    /// do so, and you take care of your own thread safety (if you share instances between threads).  
    /// In general, use <see cref="PropertyMap"/> instead since it takes care of the thread safety for you
    /// </summary>
    public class PropertyMapForType
    {
        public PropertyMapForType(Type targetType)
        {
            _offsetsToFields    = new OffsetToFieldMap(targetType);
            _fieldsToProperties = new FieldToPropertyMap(targetType);
        }

        OffsetToFieldMap _offsetsToFields;
        FieldToPropertyMap _fieldsToProperties;

        /// <summary>
        /// Gets the property that corresponds to <paramref name="field"/>
        /// </summary>
        /// <remarks>Note that <paramref name="containingObject"/> MUST be the instance that contains
        /// <paramref name="field"/>.  You will get exceptions or meaningless results if it is NOT that instance.</remarks>
        public PropertyInfo GetProperty<T>(object containingObject, ref T field)
        {
            int offset        = OffsetUtil.GetOffset(containingObject, ref field);
            string fieldName  = _offsetsToFields[offset];
            PropertyInfo prop = _fieldsToProperties[fieldName];
            return prop;
        }
    }

}
